/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NutritionUpdateAdmin.java
 *
 * Created on Aug 14, 2011, 10:59:02 PM
 */
package GUI;

import Admin.CopyFile;
import Class.Nutrition;
import Admin.InitData;
import Class.ImageFilter;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Tomcat
 */
public class NutritionUpdateAdmin extends javax.swing.JDialog {

    String nutriID;
    JFrame frame;
    private String src = "";
    private String des = "";

    /** Creates new form NutritionUpdateAdmin */
    public NutritionUpdateAdmin(String nutriID, JFrame parent, boolean modal) {
        super(parent, modal);
        this.frame = parent;
        initComponents();
        this.nutriID = nutriID;
        ResultSet rs = InitData.InitContentNutri(nutriID);
        try {
            if (rs.next()) {
                txtNutriID.setText(rs.getString(1));
                txtNutriName.setText(rs.getNString(2));
                txtaNutriContent.setText(rs.getNString(3));
                lblViewImg.setIcon(new ImageIcon(rs.getString(4)));
                txtImgName.setText(rs.getString(4));
                if (rs.getString(5).equals("1")) {
                    rbActive.setSelected(true);
                } else {
                    rbInactive.setSelected(true);
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(NutritionUpdateAdmin.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtaNutriContent = new javax.swing.JTextArea();
        txtNutriName = new javax.swing.JTextField();
        txtNutriID = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        btnBrowse = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lblViewImg = new javax.swing.JLabel();
        txtImgName = new javax.swing.JTextField();
        rbActive = new javax.swing.JRadioButton();
        rbInactive = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        btnDeleteImg = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        txtaNutriContent.setColumns(20);
        txtaNutriContent.setRows(5);
        jScrollPane1.setViewportView(txtaNutriContent);

        txtNutriID.setEditable(false);

        jLabel1.setFont(new java.awt.Font("Century Gothic", 1, 12));
        jLabel1.setText("ID :");

        jLabel2.setFont(new java.awt.Font("Century Gothic", 1, 12));
        jLabel2.setText("Title :");

        jLabel3.setFont(new java.awt.Font("Century Gothic", 1, 12));
        jLabel3.setText("Content :");

        btnSave.setFont(new java.awt.Font("Century Gothic", 1, 12));
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnReset.setFont(new java.awt.Font("Century Gothic", 1, 12));
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        btnCancel.setFont(new java.awt.Font("Century Gothic", 1, 12));
        btnCancel.setText("Cancel");

        jLabel4.setFont(new java.awt.Font("Century Gothic", 1, 12));
        jLabel4.setText("Image");

        btnBrowse.setFont(new java.awt.Font("Century Gothic", 1, 12));
        btnBrowse.setText("Browse");
        btnBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(lblViewImg);

        txtImgName.setEditable(false);

        buttonGroup1.add(rbActive);
        rbActive.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        rbActive.setText("Active");

        buttonGroup1.add(rbInactive);
        rbInactive.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        rbInactive.setText("Inactive");

        jLabel5.setFont(new java.awt.Font("Century Gothic", 1, 12));
        jLabel5.setText("Status");

        btnDeleteImg.setFont(new java.awt.Font("Century Gothic", 1, 12)); // NOI18N
        btnDeleteImg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/delete.png"))); // NOI18N
        btnDeleteImg.setText("Delete Image");
        btnDeleteImg.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteImgActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(31, 31, 31)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtNutriID, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(164, 164, 164))
                                    .addComponent(txtNutriName, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnBrowse)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtImgName, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(btnSave)
                        .addGap(18, 18, 18)
                        .addComponent(btnReset)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(rbActive)
                        .addGap(28, 28, 28)
                        .addComponent(rbInactive)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 298, Short.MAX_VALUE)
                        .addComponent(btnDeleteImg, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNutriID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNutriName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnBrowse)
                            .addComponent(txtImgName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(rbActive)
                    .addComponent(rbInactive)
                    .addComponent(btnDeleteImg, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnReset)
                    .addComponent(btnSave)
                    .addComponent(btnCancel))
                .addGap(30, 30, 30))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private boolean CheckValid() {
        String nutriName = txtNutriName.getText();
        String nutriContent = txtaNutriContent.getText();
        if (nutriName.equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(this, " Name can not blank", "ERROR", JOptionPane.OK_OPTION);
            return false;
        }
        if (nutriContent.equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(this, " Content can not blank", "ERROR", JOptionPane.OK_OPTION);
            return false;
        }
        return true;
    }
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        String nutriName = txtNutriName.getText();
        String nutriContent = txtaNutriContent.getText();
        String nutriImage = txtImgName.getText();
        String status = "";
        if (nutriImage.equals("")) { //if user doesn't choose any images
            nutriImage = "";
        } else if (!src.equals("") || !des.equals("")) { //if user choose new image
            File newFile = CopyFile.FileCopy(src, des);
            nutriImage = newFile.toString();
        }
        if (rbActive.isSelected()) {
            status = "1";
        } else {
            status = "0";
        }
        if (CheckValid()) {
            Nutrition nutri = new Nutrition(nutriID, nutriName, nutriContent, nutriImage, status);
            int resultUpdate = InitData.UpdateNutri(nutri);
            if (resultUpdate > 0) {
                JOptionPane.showMessageDialog(this, "Update Nutrition Successfully!");
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Update Nutrition Fail!", "UPDATE ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
}//GEN-LAST:event_btnSaveActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // TODO add your handling code here:
        txtNutriName.setText("");
        txtaNutriContent.setText("");
}//GEN-LAST:event_btnResetActionPerformed

    private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseActionPerformed
        JFileChooser imgChooser = new JFileChooser();
        String url = "";
        imgChooser.setCurrentDirectory(new File("src\\Images"));
        imgChooser.setFileFilter(new ImageFilter());
        int chooser = imgChooser.showOpenDialog(this);
        if (chooser == JFileChooser.APPROVE_OPTION) {
            File f = imgChooser.getSelectedFile();
            url = f.toString();
            String file = null;
            file = url.substring(url.lastIndexOf("\\"));
            this.src = url;
            this.des = "src\\Images" + file;
            txtImgName.setText(f.getName());
        }
        lblViewImg.setIcon(new ImageIcon(url));
    }//GEN-LAST:event_btnBrowseActionPerformed

    private void btnDeleteImgActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteImgActionPerformed
        if (txtImgName.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "No Image To Delete", "ERROR", JOptionPane.ERROR_MESSAGE);
        } else {
            int result = InitData.DeleteNutriImg(this.nutriID);
            if (result > 0) {
                JOptionPane.showMessageDialog(this, "Delete Image Successfully!");
                txtImgName.setText("");
                lblViewImg.setIcon(new ImageIcon(""));
            } else {
                JOptionPane.showMessageDialog(this, "Delete Image Fail!", "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnDeleteImgActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                NutritionUpdateAdmin dialog = new NutritionUpdateAdmin(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowse;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDeleteImg;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblViewImg;
    private javax.swing.JRadioButton rbActive;
    private javax.swing.JRadioButton rbInactive;
    private javax.swing.JTextField txtImgName;
    private static javax.swing.JTextField txtNutriID;
    private static javax.swing.JTextField txtNutriName;
    private static javax.swing.JTextArea txtaNutriContent;
    // End of variables declaration//GEN-END:variables
}
